package com.bohua.intermediary.service.impl;

import com.alibaba.fastjson.JSON;
import com.bohua.intermediary.common.Lay;
import com.bohua.intermediary.common.Msg;
import com.bohua.intermediary.dao.SysResourcesMapper;
import com.bohua.intermediary.dao.SysRoleMapper;
import com.bohua.intermediary.dao.SysUserMapper;
import com.bohua.intermediary.entity.SysResources;
import com.bohua.intermediary.entity.SysRole;
import com.bohua.intermediary.entity.SysUser;
import com.bohua.intermediary.security.MyUser;
import com.bohua.intermediary.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 服务实现
 * @author qian
 * @email 1415261159@qq.com
 * @date 2019/01/19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysResourcesMapper sysResourcesMapper;
    @Resource
    private PasswordEncoder passwordEncoder;

    /**
     * 人事录入员工信息
     * @param sysUser 实体
     * @return
     */
    @Override
    public Msg addSysUser(SysUser sysUser) {
        /**
         * 加密操作
         */
        sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        sysUser.setCreateTime(new Date());
        sysUser.setLocked(1);
        sysUser.setState(1);
        sysUser.setUpdateTime(new Date());
        Msg msg=new Msg();
        int i=  sysUserMapper.insert(sysUser);

        if (i>0){
            msg.setMsg("添加成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("添加失败");
        }
        return msg;
    }

    @Override
    public Msg updateSysUser(SysUser sysUser) {
        int i= sysUserMapper.updateByParam(sysUser);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("修改成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("修改失败");
        }
        return msg;
    }

    @Override
    public Msg delSysUser(SysUser sysUser) {
        int i=sysUserMapper.del(sysUser.getId());
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("删除成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("删除失败");
        }
        return msg;
    }

    @Override
    public Lay getSysUsers(SysUser sysUser, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<SysUser> sysUsers=sysUserMapper.list(sysUser);
        PageInfo info=new PageInfo(sysUsers);
        Lay lay=new Lay();
        lay.setPage(page);
        lay.setCount(info.getTotal());
        lay.setData(info.getList());
        return lay;
    }


    /**
     * 根据账号查询该用户
     * @param user 操作标识
     * @return
     */
    @Override
    public SysUser getOneUser(SysUser user) {
        return sysUserMapper.getUserlogin(user);
    }

    /**
     * security的方法，用于认证获取这个人的权限信息
     * @param username
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserDetails loadUserDetails(String username) {
        SysUser user=SysUser.builder().account(username).build();
        user=sysUserMapper.getUserlogin(user);
        /**
         * 用户拥有的一个角色
         */
        SysRole sysRole=sysRoleMapper.getOneRole(user.getRoleId());
        System.out.println("用户拥有的一个角色是："+JSON.toJSONString(sysRole) );
        /**
         * 用户所拥有的资源
         */
        List<SysResources> allResources=sysResourcesMapper.getRoleAllResource(user.getRoleId());
        List<SimpleGrantedAuthority> grantedAuthorities=new ArrayList<>();
        /**
         * 将用户的角色给security，页面就可以用用户判断
         */
        grantedAuthorities.add(new SimpleGrantedAuthority(sysRole.getRoleDoc()));
        for (SysResources item:
             allResources) {
            /**
             * 将用户拥有的权限传递进来，界面就可以根据权限判断
             */
            grantedAuthorities.add(new SimpleGrantedAuthority(item.getResourcesAlias()));
        }
       // User userDetail=new User(user.getAccount(),user.getPassword(),grantedAuthorities);
        MyUser myUser=new MyUser(user.getAccount(),user.getPassword(),grantedAuthorities);
        myUser.setMeUser(user);
        return myUser;

    }
}
